package com.music.service.impl;

import com.laoxuai.beans.annotation.Autowired;
import com.laoxuai.beans.annotation.Service;
import com.laoxuai.ibatis.util.Example;
import com.laoxuai.ibatis.util.PageInfo;

import com.laoxuai.util.StringUtils;
import com.music.dao.LyricDao;
import com.music.pojo.Lyric;
import com.music.service.LyricService;

import javax.sql.rowset.serial.SerialBlob;
import java.io.UnsupportedEncodingException;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Service
public class LyricServiceImpl implements LyricService {

    @Autowired
    private LyricDao lyricDao;

    @Override
    public int insert(Lyric lyric) {
        lyric.setIsDeleted(0);
        lyric.setCreateTime(new Date());
        if(lyric.getContent().equals("")){
            return -1;
        }
        Blob content=null;
        try {
            try {
                content = new SerialBlob(lyric.getContent().getBytes("GBK"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            return lyricDao.insertrewrite(lyric,content);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        try {
            Lyric relyric=lyricDao.getByPrimaryKey(id);
            if(relyric==null){
                return -1;//当id传入的不存在时返回-1
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            return lyricDao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Lyric lyric) {
        Integer id=lyric.getId();
        try {
            Lyric relyric=lyricDao.getByPrimaryKey(id);//当歌词不存在时返回-1
            if(relyric==null){
                return -1;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            return lyricDao.update(lyric);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Lyric getByPrimaryKey(Integer id) {
        try {
            return lyricDao.getByPrimaryKey(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Lyric> listPage(Lyric lyric, int page, int size) {
        try {
            return lyricDao.listPage(lyric, page, size);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public PageInfo<Lyric> listPageInfo(Lyric lyric, int page, int size) {
        try {
            return lyricDao.listPageInfo(lyric, page, size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Lyric> selectByExample(Lyric lyric) {
        Example example = createExmaple(lyric);
        try {
            return lyricDao.selectByExample(example);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    private Example createExmaple(Lyric lyric) {
        Example example = new Example(Lyric.class);
        Example.Criteria criteria = example.createCriteria();
        // TODO
        if(lyric != null){
            if(!StringUtils.isEmpty(lyric.getId())){
                criteria.andEqualTo("id", lyric.getId());
            }
            if(!StringUtils.isEmpty(lyric.getMusicId())){
                criteria.andEqualTo("musicId", lyric.getMusicId());
            }
            if(!StringUtils.isEmpty(lyric.getContent())){
                criteria.andEqualTo("content", lyric.getContent());
            }
            if(!StringUtils.isEmpty(lyric.getLyricOrder())){
                criteria.andEqualTo("lyricOrder", lyric.getLyricOrder());
            }
            if(!StringUtils.isEmpty(lyric.getCreateTime())){
                criteria.andEqualTo("createTime", lyric.getCreateTime());
            }
            if(!StringUtils.isEmpty(lyric.getUpdateTime())){
                criteria.andEqualTo("updateTime", lyric.getUpdateTime());
            }
            if(!StringUtils.isEmpty(lyric.getIsDeleted())){
                criteria.andEqualTo("isDeleted", lyric.getIsDeleted());
            }
        }
        return example;
    }

    /**
     * Lyric 构建查询对象
     * @param lyric
     * @return
     */
    public Example createExample(Lyric lyric){
        Example example = new Example(Lyric.class);
        Example.Criteria criteria = example.createCriteria();
        if(lyric != null){
            if(!StringUtils.isEmpty(lyric.getId())){
                criteria.andEqualTo("id", lyric.getId());
            }
            if(!StringUtils.isEmpty(lyric.getMusicId())){
                criteria.andEqualTo("musicId", lyric.getMusicId());
            }
            if(!StringUtils.isEmpty(lyric.getContent())){
                criteria.andEqualTo("content", lyric.getContent());
            }
            if(!StringUtils.isEmpty(lyric.getLyricOrder())){
                criteria.andEqualTo("lyricOrder", lyric.getLyricOrder());
            }
            if(!StringUtils.isEmpty(lyric.getCreateTime())){
                criteria.andEqualTo("createTime", lyric.getCreateTime());
            }
            if(!StringUtils.isEmpty(lyric.getUpdateTime())){
                criteria.andEqualTo("updateTime", lyric.getUpdateTime());
            }
            if(!StringUtils.isEmpty(lyric.getIsDeleted())){
                criteria.andEqualTo("isDeleted", lyric.getIsDeleted());
            }
        }
        return example;
    }
}
